home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0032_Setting-Getting BITS.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  3KB  |  97 lines

  1. {
  2. CHRIS QUARTETTI
  3.  
  4. >Is there an easy way to create a 1-bit or 2-bit data structure.  For
  5. >example, a 2-bit Type that can hold 4 possible values.  For that matter,
  6. >is there a hard way? <g>  Thanks very much -Greg
  7.  
  8.    I suppose this would qualify For the hard way-- not too flexible, but it
  9. works. It would be a bit easier to do this if you wanted a bunch of the same
  10. size Variables (ie 4 4 bit Variables, or an Array of 4*x 4 bit Variables).
  11. FWIW I used BP7 here, but TP6 and up will work. Also, it need not be Object
  12. oriented.
  13. }
  14.  
  15. Type
  16.   bitf = Object                                                                                  { split 'bits' into bitfields }
  17.     bits : Word;                         { 16 bits total }
  18.  
  19.     Function  get : Word;
  20.  
  21.     Procedure set1(value : Word);        { this will be 2 bits }
  22.     Function  get1 : Word;
  23.  
  24.     Procedure set2(value : Word);        { this will be 13 bits }
  25.     Function  get2 : Word;
  26.  
  27.     Procedure set3(value : Word);        { this will be 1 bit }
  28.     Function  get3 : Word;
  29.   end;
  30.  
  31. Function bitf.get : Word;
  32. begin
  33.   get := bits;
  34. end;
  35.  
  36. Procedure bitf.set1(value : Word);
  37. { Set the value of the first bitfield }
  38. Const
  39.   valmask  : Word = $C000;  { 11000000 00000000 }
  40.   bitsmask : Word = $3FFF;  { 00111111 11111111 }
  41. begin
  42.   value := value shl 14 and valmask;
  43.   bits  := value + (bits and bitsmask);
  44. end;
  45.  
  46. Function bitf.get1 : Word;
  47. { Get the value of the first bitfield }
  48. begin
  49.   get1 := bits shr 14;
  50. end;
  51.  
  52. Procedure bitf.set2(value : Word);
  53. { Set the value of the second bitfield }
  54. Const
  55.   valmask  : Word = $3FFE;  { 00111111 11111110 }
  56.   bitsmask : Word = $C001;  { 11000000 00000001 }
  57. begin
  58.   value := (value shl 1) and valmask;
  59.   bits  := value + (bits and bitsmask);
  60. end;
  61.  
  62. Function bitf.get2 : Word;
  63. { Get the value of the second bitfield }
  64. Const
  65.   valmask : Word = $3FFE;   { 00111111 11111110 }
  66. begin
  67.   get2 := (bits and valmask) shr 1;
  68. end;
  69.  
  70. Procedure bitf.set3(value : Word);
  71. { Set the value of the third bitfield }
  72. Const
  73.   valmask  : Word = $0001;  { 00000000 00000001 }
  74.   bitsmask : Word = $FFFE;  { 11111111 11111110 }
  75. begin
  76.   value := value and valmask;
  77.   bits  := value + (bits and bitsmask);
  78. end;
  79.  
  80. Function bitf.get3 : Word;
  81. { Get the value of the third bitfield }
  82. Const
  83.   valmask : Word = $0001;  { 00000000 00000001 }
  84. begin
  85.   get3 := bits and valmask;
  86. end;
  87.  
  88. Var
  89.   x : bitf;
  90.  
  91. begin
  92.   x.set1(3);        { set all to maximum values }
  93.   x.set2(8191);
  94.   x.set3(1);
  95.   Writeln(x.get1, ', ', x.get2, ', ', x.get3, ', ', x.get);
  96. end.
  97.